package com.itcast.springboot.waste_recovery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.springboot.request.EvaluateReq;
import com.itcast.springboot.request.OrderManage;
import com.itcast.springboot.response.MyOrderRes;
import com.itcast.springboot.response.StaffOrderRes;
import com.itcast.springboot.waste_recovery.common.OrderStatus;
import com.itcast.springboot.waste_recovery.domain.Order;
import com.itcast.springboot.waste_recovery.domain.User;
import com.itcast.springboot.waste_recovery.mapper.OrderMapper;
import com.itcast.springboot.waste_recovery.mapper.UserMapper;
import com.itcast.springboot.waste_recovery.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    OrderMapper orderMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public List<MyOrderRes> getMyOrders(Integer userId, Integer orderStatus) {
        List<MyOrderRes> myOrders = orderMapper.getMyOrders(userId, orderStatus);
        return myOrders;
    }

    @Override
    public Integer evaluate(Integer orderId, Integer orderServiceEvaluation, Integer orderAttitudeEvaluation) {
        Integer result = -1;
        Order order = orderMapper.selectById(orderId);
        if ((order.getOrderServiceEvaluation().equals(0) && order.getOrderAttitudeEvaluation().equals(0)))
                if(orderServiceEvaluation > 0 && orderServiceEvaluation <= 5)
                if((orderAttitudeEvaluation > 0 && orderAttitudeEvaluation <= 5)){

            order.setOrderServiceEvaluation(orderServiceEvaluation);
            order.setOrderAttitudeEvaluation(orderAttitudeEvaluation);
            result = orderMapper.updateById(order);
        }
        return result;
    }

    @Override
    public List<StaffOrderRes> getStaffOrder(Integer staffId, Integer orderStatus) {
        List<StaffOrderRes> staffOrderResList = orderMapper.getStaffOrderRes(staffId, orderStatus);
        return staffOrderResList;
    }

    @Override
    public Integer takeOrder(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        //将订单状态改为已接状态
        order.setOrderStatus(OrderStatus.ORDER_TAKE.statusCode());
        int result = orderMapper.updateById(order);
        return result;
    }

    @Override
    public Map<String, Integer> getEvaluate(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        Map<String, Integer> map = new HashMap<>();
        map.put("orderServiceEvaluation", order.getOrderServiceEvaluation());
        map.put("orderAttitudeEvaluation", order.getOrderAttitudeEvaluation());
        return map;
    }

    @Override
    public Integer verityPsw(Integer userId, String userPassword) {
        Integer result = -1;
        User user = userMapper.selectById(userId);
        //使用MD5对密码进行加密
        userPassword = new Md5Hash(userPassword, user.getUserPhone(), 3).toString();
        System.out.println(userPassword);
        if (userPassword.equals(user.getUserPassword())){
            //1表示密码正确
            result = 1;
        }
        return result;
    }
}
